package com.twoGroup.boot.utils;


import com.twoGroup.boot.pojo.User;
import com.twoGroup.boot.service.imp.UserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Configuration
public class UserUtils {
    public static Logger logger = LoggerFactory.getLogger(UserUtils.class);
    private UserServiceImpl userService;
    public  static boolean isEmail(String email) {
        if (email == null)
            return false;
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(email);
        if (m.matches())
            return true;
        else
            return false;
    }

    public boolean EmailStringBool(String str, int min, int max){
        logger.info("UserStringBool：str-->"+str);
        return str!=null && str.length()>=min && str.length()<=max && !str.equals("") && isEmail(str) ;
    }

    public boolean UserStringBool(String str, int min, int max){
        logger.info("UserStringBool：str-->"+str);
        return str!=null && str.length()>=min && str.length()<=max && !str.equals("");
    }

    public boolean TelephoneBool(String telephone){
        return telephone.length()==11 && telephone.charAt(0)=='1';
    }

    public boolean UserIsNull(String str, int min, int max){
        logger.info("UserIsNull：str-->"+str);
        if(str == null || str.equals("")){
            logger.info("str==null|| str.equals(\"\")");
            return true;
        }else{
            if(str.length() >= min && str.length() <= max){
                return true;
            }else{
                return false;
            }
        }
    }

    public boolean updateUser(User user) {
        boolean a;
        Integer integer = userService.queryNickname(user.getNickname());
        if (integer == 1) {
            a = false;
        } else {
            a = true;
        }
        boolean nicknameBool = UserStringBool(user.getNickname(), 2, 6);
        boolean sexBool = UserIsNull(user.getSex(), 1, 1);
        boolean ageBool = (user.getAge() >= 0 && user.getAge() <= 100);
        boolean telephoneBool = UserIsNull(user.getTelephone(), 11, 11);
        boolean addressBool = UserIsNull(user.getAddress(), 0, 255);
        boolean idCardBool = UserIsNull(user.getIdCard(), 15, 18);
        boolean rNameBool = UserIsNull(user.getrName(), 2, 4);

        if ((a = true || nicknameBool) && sexBool && ageBool && (telephoneBool || user.getTelephone().length() != 11) && addressBool && (idCardBool || user.getIdCard() == null) && (rNameBool || user.getrName() == null)) {
            return true;
        } else {
            return false;
        }
    }

    public static String md5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有md5这个算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);// // 16进制数字
        // 如果生成数字未满32位，需要前面补0
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }
}
